package pl.edu.pk.mech.kkbus.datastore.common;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.appengine.api.datastore.Entity;

public class Utils {
	private Utils(){};
	private static final Logger logger = Logger.getLogger(Utils.class.getCanonicalName());

	/**
	 * @param string
	 * @return zwracan znormalizowany lancuch znakow
	 */
	public String normalizeString(String string){
	    logger.log(Level.INFO, "Normalizacja lancucha znakow");
		String trimedLower = string.toLowerCase().trim();
		return trimedLower.replaceAll("\\W+", "-");
	}
	
	 /**
	 * @param entities
	 * @return lista encji w formacie JSON
	 */
	public static String writeJSON(Iterable<Entity> entities) {
	    logger.log(Level.INFO, "Konwertuje liste encji do formatu JSON");
	    StringBuilder sb = new StringBuilder();
	    int i = 0;
	    sb.append("{\"data\": [");
	    for (Entity result : entities) {
	      Map<String, Object> properties = result.getProperties();
	      sb.append("{");
	      if (result.getKey().getName() == null)
	        sb.append("\"name\" : \"" + result.getKey().getId() + "\",");
	      else
	        sb.append("\"name\" : \"" + result.getKey().getName() + "\",");
	
	      for (String key : properties.keySet()) {
	        sb.append("\"" + key + "\" : \"" + properties.get(key) + "\",");
	      }
	      sb.deleteCharAt(sb.lastIndexOf(","));
	      sb.append("},");
	      i++;
	    }
	    if (i > 0) {
	      sb.deleteCharAt(sb.lastIndexOf(","));
	    }
	    sb.append("]}");
	    return sb.toString();
	  }

	 /**
	 * @param entities
	 * @param childKind
	 * @param fkName
	 * @return przeksztalca encje rodzic - dziecko i zwraca string w formacie JSON
	 */
	public static String writeJSON(Iterable<Entity> entities, String childKind, String fkName) {
	    logger.log(Level.INFO,"creating JSON format object for parent child relation");
	    StringBuilder sb = new StringBuilder();
	    int i = 0;
	    sb.append("{\"data\": [");
	    for (Entity result : entities) {
	      Map<String, Object> properties = result.getProperties();
	      sb.append("{");
	      if (result.getKey().getName() == null)
	        sb.append("\"name\" : \"" + result.getKey().getId() + "\",");
	      else
	        sb.append("\"name\" : \"" + result.getKey().getName() + "\",");
	      for (String key : properties.keySet()) {
	        sb.append("\"" + key + "\" : \"" + properties.get(key) + "\",");
	      }
	      Iterable<Entity> child = SynchronizeCommon.listEntities(childKind, fkName,String.valueOf(result.getKey().getId()));
	      for (Entity en : child) {
	        for (String key : en.getProperties().keySet()) {
	          sb.append("\"" + key + "\" : \"" + en.getProperties().get(key)+ "\",");
	        }
	      }
	      sb.deleteCharAt(sb.lastIndexOf(","));
	      sb.append("},");
	      i++;
	    }
	    if (i > 0) {
	      sb.deleteCharAt(sb.lastIndexOf(","));
	    }
	    sb.append("]}");
	    return sb.toString();
	  }
}
